<?php
##############################################
#
#   活动详情页
# @ Time : 2017-3-1 - 下午11:55:14
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Home\Controller;
use Home\Model\ActivityModel;
use Home\Model\InfoSetModel;
use Home\Model\DiscussModel;
use Home\Model\UsersModel;

class ActivityDetailController extends BaseController{
	private $_activity;
	private $_activity_id;
	
	public function _initialize(){
	    parent::_initialize();
        $this->_activity_id = I('activity_id');
        $this->assign('activity_id', $this->_activity_id);
	    $this->_activity = new ActivityModel();
	    vendor('jssdk.jssdk');
	    //使用jsapi接口          
	    $jssdk = new \JSSDK("wx2c7b4284e08afd13", "0cee7a9a0fd1d31ee4e295b900ec86bf");
	    $signPackage = $jssdk->GetSignPackage();
	    //var_dump($signPackage);
	    $this->assign('signPackage', $signPackage);
	    
	}
	
	/**
	 * 商品详情页
	 */
	public function index(){
		//var_dump(MODULE_NAME);
	    //$city = I('city');
	    $city = urldecode(str_replace('|','%',I('city')));
	    $this->assign('city', $city);
	    $city = mb_substr($city,0,strlen($city)-3);
	    $activity = $this->_activity->getActivityDetail($this->_activity_id);
	    //showData($activity);
	    //推荐活动
	    self::Recommend($city);
	    //已报名
	    self::signUp();
	    //讨论
	    self::Discuss();
	    //是否关注该活动
	    self::is_collect();
	    //增加浏览量
	    self::browser();

	    $this->assign('activity', $activity);
	    $this->display('activity_detail');
	}
	
	/**
	 * 已报名人数  / 已报名列表
	 */
	public function signUp(){
	    $info_set_model = new InfoSetModel();
	    $data = $info_set_model->getDetailMoreSignUp($this->_activity_id);
	    $this->assign('person_count', $data[2]);
	    $this->assign('sign', $data[3]);
	   
	}
	
	/**
	 * 推荐活动
	 */
	public function Recommend($city) {
	    $reco_data = $this->_activity->getRecommend($city, $this->_activity_id);
	    if(count($reco_data > 0)){
	    	$this->assign('reco_show',1);
	    }
	    $this->assign('reco', $reco_data);
	}
	
	/**
	 * 更多报名
	 */
	public function moreSignUp(){
	    $info_set_model = new InfoSetModel();
	    $data = $info_set_model->getDetailMoreSignUp($this->_activity_id);
	    if(count($data[0]) > 0){
	        $this->assign('signup_show', 1);
	    }
	    $this->assign('sign', $data[0]);
	    $this->assign('sign_count', $data[2]);
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_sign_list');
	        exit;
	    }
	    $this->display('sign_list');
	}
	
	/**
	 * 讨论
	 */
	public function Discuss(){
	    $discuss_model = new DiscussModel();
	    $limit = !$_GET['is_ajax'] ? 2 : '';
	    $data = $discuss_model->getDiscuss($this->_activity_id, $limit);
	    $this->assign('discuss', $data[0]);
	    $this->assign('discuss_count', $data[1]);
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_discuss_list');
	        exit;
	    }
	    I('display') ? $this->display('discuss_list') : '';
	}
	
	/**
	 * 添加讨论
	 */
	public function addDiscuss() {
	    $discuss = array(
	        'activity_id'=>I('activity_id'), 
	        'user_id'=> $this->user_id, 
	        'nickname'=> $this->nickname,
	        'head_img'=> $this->headimgurl,
	        'content'=>I('content'),
	        'add_time'=>time()
	    );
	   I('reply_user_id') ? $discuss['reply_user_id'] = I('reply_user_id') : '';
	   I('reply_nickname') ? $discuss['reply_nickname'] = I('reply_nickname') : '';
       $discuss_model = new DiscussModel();
	   $result = $discuss_model->addDiscuss($discuss);
	   
	   $html = '<li class="item-content img_top"> <div class="item-media div_block">
	             <img src="'.$discuss['head_img'].'">
        	     </div>
        	     <div class="item-inner" onclick="discuss_form('.$discuss['user_id'].','."'".$discuss['nickname']."'".')">
        	     <div class="item-title-row">
        	     <div class="item-title">'.$discuss['nickname'].'<span class="pull-right">'.date("Y-m-d", $discuss['add_time']).'</span></div>
        	     </div>';
	   if($discuss['reply_user_id']){
	       $html .= '<div class="item-title" style="line-height:20px;text-indent: 1em;color: #999;">回复&nbsp; '.$discuss['reply_nickname'].'：</div>';
	   }
	   $html .= '<div class="item-subtitle">'.$discuss['content'].'</div></div></li>';
	   if($result){
	       echo $html;
	   }else{
	       echo false;
	   }
	}

	/**
	 * 是否会员、报名人数已满
	 */
	public function isMemberAndisMaxPeople() {
	    $user_id = I('get.user_id');
	    $activity_id = I('activity_id');
	    $users_model = new UsersModel();
	    $user_type = $users_model->is_Member($user_id);
	    $data = $this->_activity->isMaxPeople($activity_id);
	    if($user_type >= 2){
	        if($data['signup_number'] >= $data['number']){
	            $this->ajaxReturn(array('status' => 3,'message' => '抱歉，该活动报名已满！'));
	        }
	        $this->ajaxReturn(array('status' => 1,'message' => '会员'));
	    }else{
	        $this->ajaxReturn(array('status' => 2,'message' => '请先申请成为会员,再报名活动！'));
	    }
	}

	/**
	 * 关注活动
	 */
	public function collectActivity() {
	    $data = array(
	        'user_id' => I('user_id'),
	        'activity_id' => I('get.activity_id'),
	        'add_time' => time()
	    );
	    $result = M('collect')->add($data);
	    if($result)
	        $this->ajaxReturn(array('status' => 1,'message' => '关注成功！'));
	    else
	        $this->ajaxReturn(array('status' => 2,'message' => '关注失败！'));
	}
	
	/**
	  * 是否关注该活动
	 */
	public function is_collect() {
	    $result = M('collect')->where("activity_id = $this->_activity_id and user_id = $this->user_id")->count();
	    if($result)
	        $this->assign('collect', $result);
	}
	
	/**
	 * 增加浏览量
	 */
	public function browser() {
	    $this->_activity->browser($this->_activity_id);
	}

	public function share(){
		$id = I('id');
		$result = M('activity')->where("id = $id")->setInc('share_number');
		if($result)
			$this->ajaxReturn(array('status'=>1,'message'=>'分享成功'));
		else
			$this->ajaxReturn(array('status'=>2,'message'=>'分享失败'));
	}

}